// pages/AddMember/AddMember.js
import http from '../../utils/http.js';
import upload from '../../utils/upload'


import { memberEDitMemberApi } from '../../utils/newApi.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    businessId: '',
    powerArray: ['店铺管理员','员工'],
    name: '',
    phone: '',
    avatar: '',
    power: '员工',
    genderArray: ['未知','男','女'],
    gender: 0,
    genderTxt: '未知'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options.info,"optionsoptions")
    if (options.info) {
      wx.setNavigationBarTitle({
        title: "管理员工"
      })
      // const { avatar, gender, id, isAdmin, nickName: name, phone, businessId } = JSON.parse(options.info)
      console.log(options.info,"options.info")
      let dataInfo=JSON.parse(decodeURIComponent(options.info))
      console.log(dataInfo,"dataInfo")

      this.setData({
        businessId:dataInfo.businessId,
        id:dataInfo.id,
        avatar:dataInfo.adminMemberAvatar,
        gender:dataInfo.adminMemberSex,
        genderTxt: dataInfo.adminMemberSex === 1 ? '男' : dataInfo.adminMemberSex === 2 ? '女' : '未知',
        name:dataInfo.adminMemberRealName,
        phone:dataInfo.adminMemberMobile,
      })

      // console.log(JSON.parse(options.info),"JSON.parse(options.info)")
      // console.log(this.data,"this.data")
    } else {
      this.setData({
        businessId: options.businessId
      })
    }
  },
  // 姓名
  bindInputName(e){
    this.setData({
      name: e.detail.value
    })
  },
  // 手机号
  bindInputPhone(e){
    this.setData({
      phone:  e.detail.value
    })
  },
  // 头像
  uploadAvatar(){
    let that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        upload.uploadMed(tempFilePaths[0]).then(img => {
          that.setData({
            avatar: img
          })
        })
      }
    })
  },

  // 性别
  bindGenderChange(e){
    this.setData({
      gender: e.detail.value,
      genderTxt: e.detail.value == 0 ? '未知' : e.detail.value == 1 ? '男' : '女'
    })
  },
  // 权限
  bindPickerChange(e){
    this.setData({
      power: e.detail.value==1 ? '员工' : '店铺管理员'
    })
  },
  // 新增员工
  AddMemberMed(){
    let that = this
    if (that.data.phone && that.data.name && that.data.power) {
      let url = '/api/store/v1/user/register'
      let params = {
        storeId: that.data.businessId,
        phone: that.data.phone,
        realName: that.data.name,
        sex: Number(that.data.gender),
        avatar: that.data.avatar,
        // isAdmin: that.data.power == '员工' ? 2 : 1
      }
      if (this.data.id) {
        params.id = this.data.id
      }

      memberEDitMemberApi(params).then(res=>{
        if(res.code === 0){
          wx.showToast({
            title: `${this.data.id ? '修改' : '新增'}成功`,
            icon:'none',
            success() {
              setTimeout(function() {
                wx.navigateBack({
                  delta: 1
                })
              },1000)
            }
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none'
          })
        }
      })
      // http.post(url, params).then(res => {
      //   if (res.code === 200) {
      //     wx.showToast({
      //       title: `${this.data.id ? '修改' : '新增'}成功`,
      //       icon:'none',
      //       success() {
      //         setTimeout(function() {
      //           wx.navigateBack({
      //             delta: 1
      //           })
      //         },1000)
      //       }
      //     })
      //   } else {
      //     wx.showToast({
      //       title: res.message,
      //       icon: 'none'
      //     })
      //   }
      // })
    } else {
      wx.showToast({
        title: '请填写必填信息',
        icon:'none'
      })
    }
  }
})